package com.team9.fitness.controller;

import com.team9.fitness.common.ApiResponse;
import com.team9.fitness.entity.UserFoodRecord;
import com.team9.fitness.entity.FoodRecordItem;
import com.team9.fitness.service.UserFoodRecordService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/food-records")
public class UserFoodRecordController {

    private final UserFoodRecordService recordService;

    public UserFoodRecordController(UserFoodRecordService recordService) {
        this.recordService = recordService;
    }

    @PostMapping
    public ApiResponse<UserFoodRecord> saveFoodRecord(
            @RequestHeader("X-User-Id") Long userId,
            @RequestBody List<FoodRecordItem> items) {
        try {
            UserFoodRecord record = recordService.saveFoodRecord(userId, items);
            return ApiResponse.success(record);
        } catch (Exception e) {
            return ApiResponse.error("保存失败: " + e.getMessage());
        }
    }

    @GetMapping("/history")
    public ApiResponse<List<UserFoodRecord>> getFoodHistory(
            @RequestHeader("X-User-Id") Long userId) {
        try {
            List<UserFoodRecord> history = recordService.getUserFoodHistory(userId);
            return ApiResponse.success(history);
        } catch (Exception e) {
            return ApiResponse.error("获取历史记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/today")
    public ApiResponse<String> getTodayRecord(
            @RequestHeader("X-User-Id") Long userId) {
        try {
            // 这里需要实现获取今日记录的逻辑
            return ApiResponse.success("获取今日记录成功", null);
        } catch (Exception e) {
            return ApiResponse.error("获取今日记录失败: " + e.getMessage());
        }
    }
}
